package com.sskj.linechat.javatest;

import android.os.Build;

import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class test {
    @RequiresApi(api = Build.VERSION_CODES.N)
    public static void main(String[] args) {
//        Queue<User> q = new PriorityQueue<>(new UserComparator());
//        // 添加3个元素到队列:
//        q.offer(new User("Bob", "A5"));
//        q.offer(new User("Alice", "A4"));
//        q.offer(new User("Boss", "V1"));
//        q.offer(new User("a", "A10"));
//        q.offer(new User("b", "A12"));
//        q.offer(new User("c", "V12"));
//        q.offer(new User("d", "V2"));
//        q.offer(new User("e", "A3"));
//        System.out.println(q.poll()); // Boss/V1
//        System.out.println(q.poll()); // Bob/A1
//        System.out.println(q.poll()); // Alice/A2
//        System.out.println(q.poll()); // Alice/A2
//        System.out.println(q.poll()); // Alice/A2
//        System.out.println(q.poll()); // Alice/A2
//        System.out.println(q.poll()); // Alice/A2
//        System.out.println(q.poll()); // Alice/A2
//        System.out.println(q.poll()); // null,因为队列为空

//        String hex = toHex(12500);
//        System.out.println(hex);
        List<String> mutable = new ArrayList<>();
        mutable.add("apple");
        mutable.add("pear");
        // 变为不可变集合:
//        List<String> immutable = Collections.unmodifiableList(mutable);
        mutable = Collections.unmodifiableList(mutable);
        try {
            mutable.add("orange");
            System.out.println(mutable);
        } catch (Exception e) {
            System.out.println(mutable);
        }
    }

    private static String toHex(int num) {
        Deque<String> deque = new LinkedList<>();
        int i;
        while (num != 0) {
            i = num % 16;
            deque.push(toHexChar(i));
            num = num / 16;
        }

        String s = "";
        while (deque.peek() != null) {
            s = s + deque.pop();
        }
        return s;
    }

    private static String toHexChar(int num) {
        String i;
        if (num == 10) {
            i = "A";
        } else if (num == 11) {
            i = "B";
        } else if (num == 12) {
            i = "C";
        } else if (num == 13) {
            i = "D";
        } else if (num == 14) {
            i = "E";
        } else if (num == 15) {
            i = "F";
        } else {
            i = num + "";
        }
        return i;
    }
}

//A10vs:A5result：-4
//A12vs:A10result：2
class UserComparator implements Comparator<User> {
    public int compare(User u1, User u2) {
        if (u1.number.charAt(0) == u2.number.charAt(0)) {
            // 如果两人的号都是A开头或者都是V开头,比较号的大小:
            Integer sub1 = Integer.valueOf(u1.number.substring(1));
            Integer sub2 = Integer.valueOf(u2.number.substring(1));
//            int i = u1.number.compareTo(u2.number);
//            System.out.println(u1.number + "vs:" + u2.number + "result：" + i);
            return sub1.compareTo(sub2);
        }
        if (u1.number.charAt(0) == 'V') {
            // u1的号码是V开头,优先级高:
            return -1;
        } else {
            return 1;
        }
    }
}

class User {
    public final String name;
    public final String number;

    public User(String name, String number) {
        this.name = name;
        this.number = number;
    }

    public String toString() {
        return name + "/" + number;
    }
}

